home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 4 / MacMania 4.toast / / Over 200 Fonts / Misc. / gregs-hack / Antialias.c next >
Text File  |  1995-06-26  |  5KB  |  206 lines

  1. /*
  2.      Greg's Hack — MacHack '95 Best Hack Contest (22-24 June 1995 )
  3.     Copyright © 1995 Gregory D. Landweber, ALL RIGHTS RESERVED
  4. */
  5.  
  6. #include <QDOffscreen.h>
  7. #include <A4Stuff.h>
  8. #include "AntiAlias.h"
  9.  
  10. void ShrinkMap ( PixMapHandle srceMap, PixMapHandle destMap, short width, short height);
  11.  
  12. GWorldPtr    smallWorld, largeWorld;
  13. long        table[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
  14. long        table2[256];
  15. Rect        smallRect = { 0, 0, 32, 1024 },
  16.         largeRect = { 0, 0, 128, 4096 };
  17.  
  18. pascal void MyDrawChar ( char theChar )
  19. {
  20.     MyDrawText ( &theChar, 0, 1 );
  21. }
  22.  
  23. pascal void MyDrawString ( StringPtr theString )
  24. {
  25.     MyDrawText ( (Ptr)theString, 1, theString[0] );
  26. }
  27.  
  28. pascal void MyDrawText ( Ptr buffer, short offset, short len )
  29. {
  30.     GWorldPtr        saveGW;
  31.     GDHandle        saveGD;
  32.     short        txSize, txFont, txMode;
  33.     FontInfo        fInfo;
  34.     Rect            myRect, maskRect;
  35.     Point            penPt;
  36.     short        width;
  37.     short        pixelSize;
  38.     PixMapHandle    largePixMap;
  39.     Boolean        onScreen;
  40.     Boolean        colorPort;
  41.     Ptr            BaseAddr;
  42.  
  43.     EnterCodeResource();
  44.  
  45.     GetGWorld ( &saveGW, &saveGD );
  46.     pixelSize = (*(*saveGD)->gdPMap)->pixelSize;
  47.     
  48.     txFont = saveGW->txFont;
  49.     txSize = 4 * ( saveGW->txSize ? saveGW->txSize : ( LMGetSysFontSize() ? LMGetSysFontSize() : 12 ) );
  50.     txMode = saveGW->txMode;
  51.     
  52.     colorPort = ( (saveGW->portVersion & 0xC000) == 0xC000 );
  53.     BaseAddr = ((GrafPtr)saveGW)->portBits.baseAddr;
  54.     if ( colorPort )
  55.         BaseAddr = (*(PixMapHandle)BaseAddr)->baseAddr;
  56.         
  57.     onScreen = ( BaseAddr == LMGetScrnBase() );
  58.     
  59.     if (    !onScreen ||
  60.         ( saveGW->txSize >= 32 ) ||
  61.         ( txFont == 0 ) || 
  62.         ( txFont == 1/*&& txSize == 48*/ ) ||
  63.         ( txFont == 3 ) ||
  64.         ( txFont == 4 ) ||
  65.         ( ( ( pixelSize < 8 ) &&
  66.           ( ( pixelSize != 4 ) || TestDeviceAttribute ( saveGD, gdDevType ) ) ) ) ) {
  67.         
  68. #ifdef __powerc
  69.         CallUniversalProc ( oldDrawText, uppDrawTextProcInfo, buffer, offset, len );
  70. #else
  71.         ( ( DrawTextType ) oldDrawText ) ( buffer, offset, len );
  72. #endif
  73.  
  74.         ExitCodeResource();
  75.         return;
  76.     }
  77.     
  78.     if ( LMGetFractEnable() == 0 )
  79.         SetFractEnable ( true );
  80.     
  81.     GetFontInfo ( &fInfo );
  82.     
  83.     GetPen ( &penPt );
  84.  
  85.     SetGWorld ( largeWorld, 0 );
  86.     
  87.     TextFont ( txFont );
  88.     TextSize ( txSize );
  89.     
  90.     if ( LockPixels ( largePixMap = GetGWorldPixMap ( largeWorld ) ) ) {
  91.         Point        endPt;
  92.         
  93.         EraseRect ( &largeRect );
  94.         
  95.         TextFace ( saveGW->txFace );
  96.         
  97.         if ( txMode == grayishTextOr ) {
  98.             TextMode ( grayishTextOr );
  99.             txMode = srcOr;
  100.         }
  101.         else
  102.             TextMode ( srcCopy );
  103.         
  104.         MoveTo ( 0, largeRect.top + 4 * ( saveGW->txSize + 1 - fInfo.descent) );
  105.  
  106. #ifdef __powerc
  107.         CallUniversalProc ( oldDrawText, uppDrawTextProcInfo, buffer, offset, len );
  108. #else
  109.         ( ( DrawTextType ) oldDrawText ) ( buffer, offset, len );
  110. #endif
  111.         
  112.         GetPen ( &endPt );
  113.         width = ( endPt.h + 3 ) / 4;
  114.         
  115.         myRect.left    = penPt.h;
  116.         myRect.bottom    = penPt.v + fInfo.descent;
  117.         myRect.right    = myRect.left + width + 1;
  118.         myRect.top    = myRect.bottom - saveGW->txSize - 1;
  119.         
  120.         maskRect = myRect;
  121.         OffsetRect ( &maskRect, - maskRect.left, - maskRect.top );
  122.         {
  123.             PixMapHandle    smallPixMap, savePixMap;
  124.             
  125.             if ( !LockPixels ( smallPixMap = GetGWorldPixMap ( smallWorld ) ) )
  126.                 SysBeep ( 10 );
  127.             if ( !LockPixels ( savePixMap  = GetGWorldPixMap ( saveGW ) ) )
  128.                 SysBeep ( 10 );
  129.             
  130.             ShrinkMap ( largePixMap, smallPixMap, maskRect.right, maskRect.bottom );
  131.             SetGWorld ( saveGW, saveGD );
  132.             CopyBits ( (BitMap *)*smallPixMap, (BitMap *)*savePixMap,
  133.                        &maskRect, &myRect, txMode, 0L );
  134.             UnlockPixels ( smallPixMap );
  135.             UnlockPixels ( savePixMap);
  136.         }
  137.         UnlockPixels ( largePixMap );
  138.     }
  139.         
  140.     Move ( width, 0 );
  141.     
  142.     ExitCodeResource();    // hack
  143. }
  144.                     
  145. void ShrinkMap ( PixMapHandle srceMap, PixMapHandle destMap, short width, short height )
  146. {
  147.     Byte    *srceBase, *destBase, *destBase0,
  148.         *srceBase0, *srceBase1, *srceBase2, *srceBase3;
  149.     
  150.     short    row, byte;
  151.     short    rowBytes, numBytes;
  152.     long        *table3 = table2;
  153.     
  154.     if ( width >1024 )
  155.         width = 1024;
  156.     
  157.     if ( height > 32 )
  158.         height = 32;
  159.         
  160.     rowBytes = (*destMap)->rowBytes & 0x7FFF;
  161.     numBytes = ( ( width * 4 + 15 ) >> 4 ) << 1;
  162.     
  163.     srceBase = (Byte *)GetPixBaseAddr ( srceMap );
  164.     destBase = (Byte *)GetPixBaseAddr ( destMap );
  165.     destBase0 = destBase;
  166.     
  167.     if ( numBytes > rowBytes )
  168.         numBytes = rowBytes;
  169.  
  170.     for ( row = 0; row < height; row++ ) {
  171.         srceBase3 = rowBytes + ( srceBase2 = rowBytes + ( srceBase1 = rowBytes + ( srceBase0 = srceBase ) ) );
  172.         for ( byte = numBytes; byte > 0; byte-- ) {
  173.             long    result;
  174.             result = table3[*(srceBase0++)] + table3[*(srceBase1++)] + table3[*(srceBase2++)] +table3[*(srceBase3++)];
  175.             result -= ( result & 0x00001010 ) >> 4;
  176.             *(destBase0++) = result + (result >> 4);
  177.         }
  178.         srceBase += rowBytes << 2;
  179.         destBase0 = ( destBase += rowBytes );
  180.     }
  181. }
  182.  
  183. Boolean AntiAlias ( void )
  184. {
  185.     CTabHandle    ctab = GetCTable ( 4 + 32);
  186.     
  187.     {    long    i, j;
  188.         for ( i = 0; i < 16; i++ )
  189.             for ( j = 0; j < 16; j++ )
  190.                 table2[(i << 4 ) + j] = ( table[i] << 8 ) + table[j];
  191.     }
  192.  
  193.     if ( NewGWorld( &smallWorld, 4, &smallRect, ctab, 0, 0 ) != noErr ) {
  194.         DisposHandle( (Handle)ctab );
  195.         return false;
  196.     }
  197.     
  198.     if ( NewGWorld( &largeWorld, 1, &largeRect, 0, 0, 0 ) != noErr ) {
  199.         DisposeGWorld ( smallWorld );
  200.         return false;
  201.     }
  202.                     
  203.     SetFractEnable ( true );
  204.     
  205.     return true; 
  206. }